﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Billing.Dtos;
using Billing.Windows;
using Billing.Utilities;

namespace Billing.Controls
{
    public partial class BillingGridItemControl : UserControl
    {
        //List<BillingGridItemDto> m_AllBillingItems;
        BillingGrid m_formBillingGrid;

        #region Constructor

        public BillingGridItemControl()
        {
            InitializeComponent();
            //Mouse.OverrideCursor = Cursors.None;
            //m_AllBillingItems = allBillingItems;
        }

        public BillingGridItemControl(BillingGrid grid) : this()
        {
            m_formBillingGrid = grid;
            //Mouse.OverrideCursor = Cursors.None;
        }

        #endregion

        #region Events

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            txtCode.Focus();
        }

        private void txtCode_GotFocus(object sender, RoutedEventArgs e)
        {
            txtCode.Background = Brushes.White;
            txtCode.Foreground = Brushes.Black;
        }

        private void txtCode_LostFocus(object sender, RoutedEventArgs e)
        {
            txtCode.Background = Brushes.Transparent;
            txtCode.Foreground = Brushes.White;
        }

        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                txtQty.Background = Brushes.White;
                txtQty.Foreground = Brushes.Black;

                if (txtCode.Text != string.Empty)
                {
                    BillingGridItemDto itemDetails = CacheManager.BillingItems.Find(item => item.Code == GetCode());
                    if (itemDetails != null)
                    {
                        if (txtQty.Text != string.Empty)
                        {
                            txtPrice.Text = (Convert.ToInt64(txtQty.Text.Trim()) * itemDetails.Price).ToString();
                            vat.Text = itemDetails.VAT;
                        }
                    }
                }

                if (txtQty != null && m_formBillingGrid != null)
                    m_formBillingGrid.RefreshAllFields();

                if (m_formBillingGrid != null && txtCode.Text != string.Empty)
                {
                    m_formBillingGrid.RefreshAllFields();
                }
            }
            catch (Exception) { }
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            txtQty.Background = Brushes.Transparent;
            txtQty.Foreground = Brushes.White;
        }

        private void txtCode_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                //if (m_AllBillingItems.Count == 0)
                //    m_AllBillingItems = CacheManager.BillingItems;
                BillingGridItemDto itemDetails = CacheManager.BillingItems.Find(item => item.Code == GetCode());
                if (itemDetails != null)
                {
                    txtDesc.Text = itemDetails.Name.Trim();
                    grp.Text = itemDetails.GroupID.ToString();
                    slip.Text = itemDetails.IsSlipRequired.ToString();
                    acceptDecimalStock.Text = itemDetails.AcceptDecimalStock.ToString();
                    //txtPrice.Text = itemDetails.Price.ToString();
                    //txtQty.Text = "1";
                    vat.Text = itemDetails.VAT;
                }
                else
                {
                    txtDesc.Text = string.Empty;
                }
                //else if (itemDetails == null)
                //{
                //    if (txtCode.Text != string.Empty)
                //        MessageBox.Show("No item with this code", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                //    txtCode.Clear();
                //    txtDesc.Clear();
                //    txtPrice.Clear();
                //    txtQty.Text = "0";
                //}
            }
            catch (Exception) { }
        }

        private void txtQty_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                if (txtQty.Text.Contains(".") && txtQty.Text.Length == 2 && txtQty.Text.IndexOf(".") == 0)
                {
                    (sender as TextBox).Text = "0" + txtQty.Text;
                }
                if (!txtQty.Text.Contains(".") || (txtQty.Text.Contains(".") && txtQty.Text.Length > 2))
                {
                    if (txtCode.Text != string.Empty && txtQty.Text != string.Empty)
                    {
                        BillingGridItemDto itemDetails = CacheManager.BillingItems.Find(item => item.Code == GetCode());
                        if (itemDetails != null)
                        {
                            if (txtQty.Text.Contains(".") && txtQty.Text.Length > 2)
                            {
                                if (txtQty.Text.IndexOf(".") == 0)
                                    (sender as TextBox).Text = "0" + txtQty.Text;
                                txtQty.CaretIndex = txtQty.Text.Length;
                            }

                            double qty = Convert.ToDouble(txtQty.Text.Trim());
                            if (!(itemDetails.AcceptDecimalStock))
                            {
                                qty = Math.Ceiling(qty);
                                (sender as TextBox).Text = qty.ToString();
                            }
                            else
                            {
                                qty = Math.Round(qty, 2);
                                (sender as TextBox).Text = qty.ToString();
                                txtQty.CaretIndex = txtQty.Text.Length;
                            }
                            txtPrice.Text = (qty * itemDetails.Price).ToString();
                            acceptDecimalStock.Text = itemDetails.AcceptDecimalStock.ToString();
                            vat.Text = itemDetails.VAT;
                        }
                    }
                    else if (txtQty.Text == string.Empty)
                    {
                        txtPrice.Text = string.Empty;
                    }

                    if (txtQty != null && txtQty.Text != string.Empty && m_formBillingGrid != null)
                        m_formBillingGrid.RefreshAllFields();

                    if (m_formBillingGrid != null && txtCode.Text != string.Empty)
                    {
                        m_formBillingGrid.RefreshAllFields();
                    }
                }
            }
            catch (Exception) { }
        }

        #endregion

        #region private Methods

        int GetCode()
        {
            try
            {
                return (txtCode == null || txtCode.Text == string.Empty) ? 0 : Convert.ToInt32(txtCode.Text.Trim().ToString());
            }
            catch (Exception) 
            {
                return 0;
            }
        }

        bool processCodeKeys(KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.F1 || e.Key == Key.Delete || e.Key == Key.Escape || e.Key == Key.Enter || e.Key == Key.Back || e.Key == Key.Left || e.Key == Key.Right ||
                    e.Key == Key.D0 || e.Key == Key.D1 || e.Key == Key.D2 || e.Key == Key.D3 || e.Key == Key.D4 || e.Key == Key.D5 || e.Key == Key.D6 || e.Key == Key.D7 || e.Key == Key.D8 || e.Key == Key.D9 ||
                    e.Key == Key.F1 || e.Key == Key.F2 || e.Key == Key.F3 || e.Key == Key.F4 || e.Key == Key.F5 || e.Key == Key.F6 || e.Key == Key.F7 || e.Key == Key.F8 || e.Key == Key.F9 || e.Key == Key.F11 || e.Key == Key.F12 ||
                    e.Key == Key.NumPad0 || e.Key == Key.NumPad1 || e.Key == Key.NumPad2 || e.Key == Key.NumPad3 || e.Key == Key.NumPad4 || e.Key == Key.NumPad5 || e.Key == Key.NumPad6 || e.Key == Key.NumPad7 || e.Key == Key.NumPad8 || e.Key == Key.NumPad9)
                    return true;
            }
            catch (Exception) { }
            return false;
        }

        bool processQuantityKeys(KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.F1 || e.Key == Key.Delete || e.Key == Key.Escape || e.Key == Key.Enter || e.Key == Key.Back || e.Key == Key.OemPeriod || e.Key == Key.Left || e.Key == Key.Right ||
                    e.Key == Key.D0 || e.Key == Key.D1 || e.Key == Key.D2 || e.Key == Key.D3 || e.Key == Key.D4 || e.Key == Key.D5 || e.Key == Key.D6 || e.Key == Key.D7 || e.Key == Key.D8 || e.Key == Key.D9 ||
                    e.Key == Key.F1 || e.Key == Key.F2 || e.Key == Key.F3 || e.Key == Key.F4 || e.Key == Key.F5 || e.Key == Key.F6 || e.Key == Key.F7 || e.Key == Key.F8 || e.Key == Key.F9 || e.Key == Key.F11 || e.Key == Key.F12 ||
                    e.Key == Key.NumPad0 || e.Key == Key.NumPad1 || e.Key == Key.NumPad2 || e.Key == Key.NumPad3 || e.Key == Key.NumPad4 || e.Key == Key.NumPad5 || e.Key == Key.NumPad6 || e.Key == Key.NumPad7 || e.Key == Key.NumPad8 || e.Key == Key.NumPad9 || e.Key == Key.Decimal)
                    return true;
            }
            catch (Exception) { }
            return false;
        }

        private void txtCode_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (!processCodeKeys(e))
                    e.Handled = true;
            }
            catch (Exception) { }
        }

        private void txtQty_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (!processQuantityKeys(e))
                    e.Handled = true;
            }
            catch (Exception) { }
        }

        #endregion

        private void txtCode_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                Storage.CanPrint = false;
                if (e.Key == Key.Enter)
                {
                    BillingGridItemDto itemDetails = CacheManager.BillingItems.Find(item => item.Code == GetCode());
                    if (itemDetails != null)
                    {
                        txtDesc.Text = itemDetails.Name.Trim();
                        txtPrice.Text = itemDetails.Price.ToString();
                        txtQty.Text = "1";
                        grp.Text = itemDetails.GroupID.ToString();
                        acceptDecimalStock.Text = itemDetails.AcceptDecimalStock.ToString();
                        vat.Text = itemDetails.VAT;
                    }
                    else if (itemDetails == null)
                    {
                        if (txtCode.Text != string.Empty)
                            MessageBox.Show("No item with this code", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                        txtCode.Clear();
                        txtDesc.Clear();
                        txtPrice.Clear();
                        txtQty.Text = "1";
                        Storage.CanPrint = true;
                    }
                }
            }
            catch (Exception) { }
        }

        private void txtQty_LostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                if (txtCode.Text != string.Empty && txtQty.Text != string.Empty)
                {
                    BillingGridItemDto itemDetails = CacheManager.BillingItems.Find(item => item.Code == GetCode());
                    if (itemDetails != null)
                    {
                        double qty = Convert.ToDouble(txtQty.Text.Trim());
                        if (!(itemDetails.AcceptDecimalStock))
                        {
                            qty = Math.Ceiling(qty);
                            (sender as TextBox).Text = qty.ToString();
                        }
                        txtPrice.Text = (qty * itemDetails.Price).ToString();
                        vat.Text = itemDetails.VAT;
                    }
                }
                else if (txtQty.Text == string.Empty)
                {
                    txtPrice.Text = string.Empty;
                }

                if (txtQty != null && txtQty.Text != string.Empty && m_formBillingGrid != null)
                    m_formBillingGrid.RefreshAllFields();

                if (m_formBillingGrid != null && txtCode.Text != string.Empty)
                {
                    m_formBillingGrid.RefreshAllFields();
                }
            }
            catch (Exception)
            {
                (sender as TextBox).Text = string.Empty;
            }
        }
    }
}
